package org.hackystat.ui.chart.jfc.test;

//import org.hackystat.ui.chart.jfc.chartresource.ChartManager;
import org.hackystat.ui.chart.jfc.fileresource.ChartFileManager;
import org.hackystat.ui.chart.jfc.server.Server;
import org.junit.BeforeClass;
import org.restlet.Client;
import org.restlet.data.MediaType;
import org.restlet.data.Method;
import org.restlet.data.Preference;
import org.restlet.data.Protocol;
import org.restlet.data.Reference;
import org.restlet.data.Request;
import org.restlet.data.Response;
import org.restlet.resource.Representation;

/**
 * Provides helpful utility methods to Chart test classes, which will normally want to extend this
 * class.
 *
 * @author Pavel Senin.
 *
 */
public class UIChartServerApiHelper {

  /** The SensorBase server used in these tests. */
  private static Server server;

  /** The Client instance used in these tests. */
  private static Client client;

  private static String tempStorage;

  private static Integer portNum;

//  protected static ChartManager ChartManager;

  protected static ChartFileManager chartFileManager;

  /**
   * Starts the server going for these tests.
   *
   * @throws Exception If problems occur setting up the server.
   */
  @BeforeClass
  public static void setupServer() throws Exception {

    UIChartServerApiHelper.tempStorage = System.getProperty("user.dir") + "/test/staticplots";
    UIChartServerApiHelper.portNum = 3128;

    UIChartServerApiHelper.server = Server.newInstance(UIChartServerApiHelper.portNum,
        UIChartServerApiHelper.tempStorage);
    UIChartServerApiHelper.client = new Client(Protocol.HTTP);

//    UIChartServerApiHelper.ChartManager = (ChartManager) server.getContext().getAttributes().get(
//        "ChartManager");
    UIChartServerApiHelper.chartFileManager = (ChartFileManager) server.getContext().getAttributes()
        .get("ChartFileManager");
  }

  /**
   * Does the housekeeping for making HTTP requests to the Server.
   *
   * @param method The type of Method.
   * @param requestString A string, such as "users".
   * @return The Response instance returned from the server.
   */
  protected Response makeChartFileRequest(Method method, String requestString) {
    return makeRequest(method, requestString, null);
  }

  /**
   * Does the housekeeping for making HTTP requests to the Server by a user.
   *
   * @param method The type of Method.
   * @param requestString A string, such as "users".
   * @param entity The representation to be sent with the request.
   * @return The Response instance returned from the server.
   */
  private Response makeRequest(Method method, String requestString, Representation entity) {
    String hostName = UIChartServerApiHelper.server.getHostName();
    Reference reference = new Reference(hostName + requestString);
    Request request = (entity == null) ? new Request(method, reference) : new Request(method,
        reference, entity);
    Preference<MediaType> pngMedia = new Preference<MediaType>(MediaType.IMAGE_ALL);
    request.getClientInfo().getAcceptedMediaTypes().add(pngMedia);
    return client.handle(request);
  }

  /**
   * Returns the hostname associated with this test server.
   *
   * @return The host name, including the context root.
   */
  protected String getHostName() {
    return UIChartServerApiHelper.server.getHostName();
  }

}
